package com.shareach.dao.redis.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import redis.clients.jedis.Tuple;

import com.shareach.domain.DiscountType;
import com.shareach.redis.RedisService;

/**
 * 上架队列
 * @author shareach
 *
 */
@Repository
public class DeployRDao {
	@Autowired
	private RedisService redisService;

	/**
	 * 【对应实体】上架时间
	 * SortedSet，第一个参数是【对应实体】类型（DeployType）
	 * value是shopId,Score是上架时间
	 */
	private static final String Deploy_Shop = "dep:%s:time";
	/**
	 * 等待上架的【对应实体】，新增、更新了【对应实体】相关信息，都应该添加到此队列
	 * SortedSet,上架成功则移除，同时更新【对应实体】上架时间列表
	 */
	private static final String Deploy_ShopUpdate = "dep:%s:que";
	/**
	 * 没有匹配到坐标或者地址的
	 * SortedSet
	 * value是shopId,Score是错误类型
	 */
	private static final String Deploy_Error = "dep:%s:err";

	
	
	/**
	 * 微博的点评
	 * SortedSet 
	 * value 是微博Id， score是采集成功的时间
	 */
	private static final String Deploy_Weibo_Photo = "dep:wb:photo";
	
	
	/**
	 * 新增需要上架的实体
	 * @param type
	 * @param id 指定实体Id
	 */
	public void addDeploy(DeployType type, long id){
		String key =String.format(Deploy_ShopUpdate, type.getCode());
		String sId = String.valueOf(id);
		redisService.getRedis().addSortedSet(key, sId);
	}
	/**
	 * 检查对应实体是否已经上架
	 * @param type
	 * @param id
	 * @return
	 */
	public boolean isDeployed(DeployType type, long id){
		String key =String.format(Deploy_Shop, type.getCode());
		String sId = String.valueOf(id);
		return redisService.getRedis().isContainsSortedSet(key, sId);
	}
	/**
	 * 实体上架成功
	 * @param type
	 * @param id
	 */
	public void setDeployed(DeployType type, long id){
		String key =String.format(Deploy_Shop, type.getCode());
		String sId = String.valueOf(id);
		String keyUpdate =String.format(Deploy_ShopUpdate, type.getCode());
		//移除待上架队列
		redisService.getRedis().removeSortedSet(keyUpdate, sId);
		//更新已经商家时间
		redisService.getRedis().addSortedSet(key, sId);
	}
	
	/**
	 * 实体下架
	 * @param type
	 * @param id
	 */
	public void setUndeployed(DeployType type, long id){
		String key =String.format(Deploy_Shop, type.getCode());
		String sId = String.valueOf(id);
		String keyUpdate =String.format(Deploy_ShopUpdate, type.getCode());
		//移除上架队列
		redisService.getRedis().removeSortedSet(key, sId);
		//加入待上架队列
		redisService.getRedis().addSortedSet(keyUpdate, sId);
	}
	
	public Set<Tuple> getErrDeployList(DeployType type, int page, int pageSize) {
		String key = String.format(Deploy_Error, type.getCode());
		return redisService.getRedis().getSetScoresRangeWithScores(key, (page - 1) * pageSize, pageSize);
	}
	
	public long getErrDeployListSize(DeployType type) {
		String key = String.format(Deploy_Error, type.getCode());
		return redisService.getRedis().getSortedSetCount(key);
	}
	
	/**
	 * 获取需要上架的实体列表
	 * @param type
	 * @return
	 */
	public List<Long> getDeployList(DeployType type){
		String key =String.format(Deploy_ShopUpdate, type.getCode());
		Set<String> set = redisService.getRedis().getSetScores(key, 0, 100);
		List<Long> results = new ArrayList<Long>();
		if(set!=null){
			for(String s:set){
				results.add(Long.parseLong(s));
			}
		}
		return results;
	}

	/**
	 * 移除待上架的实体，应该同时放入等待匹配的队列
	 * @param type
	 * @param id
	 */
	public void removeDeployWait(DeployType type, long id){
		String key =String.format(Deploy_ShopUpdate, type.getCode());
		String sId = String.valueOf(id);
		redisService.getRedis().removeSortedSet(key, sId);
	}

	/**
	 * 当实体删除时调用
	 * @param type
	 * @param id
	 */
	public void removeDeploy(DeployType type, long id){
		String key =String.format(Deploy_Shop, type.getCode());
		String sId = String.valueOf(id);
		redisService.getRedis().removeSortedSet(key, sId);
	}

	/**
	 * 没有坐标的错误商户地址
	 * @param type
	 * @param id
	 */
	public void setShopDeployError(ShopDeployErrorType type,long id){
		String key =String.format(Deploy_Error, DeployType.Shop.getCode());
		String sId = String.valueOf(id);
		
		String keyUpdate =String.format(Deploy_ShopUpdate, DeployType.Shop.getCode());
		//移除待上架队列
		redisService.getRedis().removeSortedSet(keyUpdate, sId);
		
		//设置商户上架错误
		redisService.getRedis().addSortedSet(key, sId,type.getValue());
		
	}
	
	/**
	 * 没有坐标的错误商户地址
	 * @param type
	 * @param id
	 */
	public void setDeployError(DeployType type,long id){
		String key =String.format(Deploy_Error, type.getCode());
		String sId = String.valueOf(id);
		
		String keyUpdate =String.format(Deploy_ShopUpdate, type.getCode());
		//移除待上架队列
		redisService.getRedis().removeSortedSet(keyUpdate, sId);
		
		//设置商户优惠的商户上架错误
		redisService.getRedis().addSortedSet(key, sId);
	}
	
	
	/**
	 * 添加到未采集的队列
	 * @param poiid
	 */
	public void addDeploy(String poiid){
		if(StringUtils.isBlank(poiid)) return;
		redisService.getRedis().addSortedSet(Deploy_Weibo_Photo, poiid, 0);
	}
	/**
	 * 设置微博采集成功
	 * @param poiid
	 */
	public void setDeployedWeiboPhotos(String poiid){
		redisService.getRedis().addSortedSet(Deploy_Weibo_Photo, poiid);
	}

	/**
	 * 获取需要采集的微博Id列表
	 * @return
	 */
	public Set<String> getUndeployWeiboPhotoList(){
		//每隔一个小时获取一次
		return redisService.getRedis().getSetScores(Deploy_Weibo_Photo, 0, ((new Date()).getTime() - 24*60*60 * 1000), 100);
	}
	
	/**
	 * 获取已经采集的微博Id列表
	 * @return
	 */
	public Set<String> getDeployWeiboPhotoList(){
		return redisService.getRedis().getSetScores(Deploy_Weibo_Photo, 1000000, ((new Date()).getTime() + 120*60 * 1000), 100);
	}
	
	/**
	 * @author Shareach
	 * 商户上架错误类型
	 */
	public enum ShopDeployErrorType{
		None(0,"None","错误"),
		NoAddr(10,"noAddr","无地址"),
		NoLoc(20,"noLoc","无坐标")
		;
		private final int value;
		private final String code;
		private final String text;
		
		ShopDeployErrorType(int value, String code,String text) {
			this.value = value;
			this.text = text;
			this.code= code;
		}
		public int getValue() {
			return this.value;
		}
		public String getCode(){
			return this.code;
		}
		public String getText(){
			return this.text;
		}
		
		public static DiscountType get(int value) {
			for (DiscountType src : DiscountType.values()) {
				if (value == src.getValue()) {
					return src;
				}
			}
			return DiscountType.None;
		}
		public static DiscountType get(String code){
			for (DiscountType src : DiscountType.values()) {
				if (src.getCode().equals(code)) {
					return src;
				}
			}
			return DiscountType.None;
		}
	}
	/**
	 * 上架实体类型
	 * @author shareach
	 *
	 */
	public enum DeployType{
		None(0,"None","错误","none"),
		Place(2,"place","地标位置","place"),
		City(3, "city", "城市","city"),
		Bank(4, "bank", "银行","bank"),
		Shop(10,"sh","商户","shop"),
		ShopBrand(11,"brand","连锁品牌","shopBrand"),
		ShopCategory(12,"cate","商户类别","shopCategory"),
		Tuan(20,"tuan","团购","tuan"),
		TuanShop(21,"tuans","团购商户","tuanShop"),
		Card(30,"bnk","卡惠","card"),
		Activity(40,"act","活动","activity"),
		ActivityShop(41,"actS","活动商户","activityShop"),
		Ticket(50,"tkt","券","ticket")
		;
		private final int value;
		private final String code;
		private final String text;
		private final String desc;
		DeployType(int value, String code,String text,String desc) {
			this.value = value;
			this.text = text;
			this.code= code;
			this.desc=desc;
		}
		public int getValue() {
			return this.value;
		}
		public String getCode(){
			return this.code;
		}
		public String getText(){
			return this.text;
		}
		public String getDesc(){
			return this.desc;
		}
		
		public static DiscountType get(int value) {
			for (DiscountType src : DiscountType.values()) {
				if (value == src.getValue()) {
					return src;
				}
			}
			return DiscountType.None;
		}
		public static DiscountType get(String code){
			for (DiscountType src : DiscountType.values()) {
				if (src.getCode().equals(code)) {
					return src;
				}
			}
			return DiscountType.None;
		}
	}
	
}
